package com.huiminpay.merchant.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.huiminpay.common.cache.domain.PageVO;
import com.huiminpay.merchant.convert.StoreConvert;
import com.huiminpay.merchant.entity.Store;
import com.huiminpay.merchant.mapper.StoreMapper;
import com.yh.merchant.api.Service.IStoreService;
import com.yh.merchant.api.dto.QueryStoreDTO;
import com.yh.merchant.api.dto.StoreDTO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @ClassName : StoreServiceImpl
 * @Author : 阿泽
 * @Date: 2021/8/11 20:51
 * @Description :
 */
@Service
public class StoreServiceImpl implements IStoreService {

    @Autowired
    StoreMapper storeMapper;
    @Override
    public PageVO<StoreDTO> queryStoreByPage(QueryStoreDTO queryStoreDTO) {
        Page<Store> page = new Page<>(queryStoreDTO.getPageNo(), queryStoreDTO.getPageSize());

        QueryWrapper<Store> qw = new QueryWrapper<>();
        qw.lambda().eq(Store::getMerchantId,queryStoreDTO.getMerchantId());
        IPage<Store> storeIPage = storeMapper.selectPage(page, qw);

        List<StoreDTO> storeDTOS = StoreConvert.INSTANCE.listEntity2ListDto(storeIPage.getRecords());
        PageVO<StoreDTO> pageVO = new PageVO<>();
        pageVO.setCounts(storeIPage.getTotal());
        pageVO.setPage(queryStoreDTO.getPageNo());
        pageVO.setPageSize(queryStoreDTO.getPageSize());
        pageVO.setItems(storeDTOS);
        return pageVO;
    }

    @Override
    public StoreDTO selectById(Long storeId) {
        Store store = storeMapper.selectById(storeId);
        StoreDTO storeDTO = new StoreDTO();
        BeanUtils.copyProperties(store,storeDTO);
        return storeDTO;
    }
}
